package service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.DateKits;
import common.kit.PageSql;
import enums.MaintainRecordsType;
import model.Calibration;
import model.DaliyRecords;
import model.MaintainRecords;
import model.SampleRecord;
import model.Staff;
import model.Station;

public class MaintainRecordsService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tmr.id,tmr.createTime,tmr.type,tmr.recordsName,tmr.deadline,tmr.result,ts.name,tc.name AS companyName1,tsr.name AS servicerName,tmr.recorder " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_maintain_records tmr ") ;
	    pageSql.join(" t_station ts " , " ts.id = tmr.stationId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tmr.servicerId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
		pageSql.setNotLessThanCondition("tmr.createTime","tmr.createTime.start") ; //创建时间
		pageSql.setNotMoreThanCondition("tmr.createTime","tmr.createTime.end") ; //创建时间
		pageSql.setEqualCondition("tmr.stationId") ; //点位	 
		pageSql.setEqualCondition("tmr.type") ; //类型	 
		pageSql.setLikeCondition("ts.name") ; //点位名称	 
		pageSql.setLikeCondition("tsr.name") ; //运维公司	 
		pageSql.setLikeCondition("tc.name") ; //企业名称	 
	    pageSql.orderBy(" tmr.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		for (Record record : page.getList()) {
			record.set("typeStr", MaintainRecordsType.getTitleByValue(record , "type"));
		}
	    return page ;
	}

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tmr.id,tmr.createTime,tmr.recorder,tmr.type,tmr.recordsName,tmr.deadline,tmr.result,ts.name " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_maintain_records tmr ") ;
	    pageSql.join(" t_servicer ts " , " ts.id = tmr.servicerId ") ;
		pageSql.setEqualCondition("tmr.stationId") ; //点位	 
		pageSql.setEqualCondition("tmr.type") ; //类型	 
	    pageSql.orderBy(" tmr.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		for (Record record : page.getList()) {
			record.set("typeStr", MaintainRecordsType.getTitleByValue(record , "type"));
			record.set("deadline", DateKits.toStr(record.getDate("deadline")));
		}
	    return page ;
	}
	
	public Ret recentRecords(Station station) {
		Long stationId = station.getId() ;
		List<Map<String, Object>> list = MaintainRecords.listByType(station.getType()) ;
		List<Record> datasList = new ArrayList<Record>();
		for (Map<String, Object> map : list) {
			String title = (String) map.get("title");
			int type = (int) map.get("value") ;
			Record record = MaintainRecords.dao.findRecentRecords(type, stationId);
			if (null == record) {
				record = new Record();
				record.set("hasData", false);
				record.set("title", title);
				datasList.add(record);
				continue ;
			}
			record.set("deadline", DateKits.toStr(record.getDate("deadline")));
			record.set("hasData", true);
			record.set("title", title);
			datasList.add(record);
		}
		return Ret.create("list" , datasList) ;
	}

	public Ret save(MaintainRecords maintainRecords , Station station , Staff staff) {
		maintainRecords.setStationId(station.getId());
		maintainRecords.setServicerId(staff.getServicerId());
		maintainRecords.setRecorder(staff.getName());
		maintainRecords.save();
	    return ok() ;
	}
	
	/**
	 * 保存维护记录
	 */
	public void saveMaintain( DaliyRecords daliyRecords , String factors , Integer days , Station station , Staff staff) {
		MaintainRecords maintainRecords = new MaintainRecords();
		maintainRecords.setMaintainType();
		maintainRecords.setResult(daliyRecords.getIsNormal() ? "正常" : "异常");
		maintainRecords.setRecordsName(factors);
		maintainRecords.setDeadline( DateKits.addDay(days) );
		this.save(maintainRecords, station, staff);
	}
	
	
	public void saveQutity(SampleRecord sampleRecord , Station station , Staff staff) {
		MaintainRecords maintainRecords = new MaintainRecords();
		maintainRecords.setQualityControlType();
		maintainRecords.setResult(sampleRecord.getIsNormal() ? "正常" : "异常");
		maintainRecords.setRecordsName(sampleRecord.getProjectName());
		this.save(maintainRecords, station, staff);
	}
	
	public void saveCompare(String name , String result , Station station , Staff staff) {
		MaintainRecords maintainRecords = new MaintainRecords();
		maintainRecords.setCompareType();
		maintainRecords.setResult(result);
		maintainRecords.setRecordsName(name);
		this.save(maintainRecords, station, staff);
	}
	
	public void saveGasQuatity(Calibration calibration , Station station , Staff staff) {
		MaintainRecords maintainRecords = new MaintainRecords();
		maintainRecords.setQualityControlType();
		String isNormal = calibration.getIsNormal() ? "正常" : "异常" ;
		String isENormal = calibration.getRangeIsNormal() ? "正常" : "异常" ;
		maintainRecords.setResult( "零点漂移校准" + isNormal + "、量程漂移校准"  + isENormal);
		maintainRecords.setRecordsName(calibration.getPrinciple());
		this.save(maintainRecords, station, staff);
	}
	
}
